import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'

// 静态路由
const routes = [
    {
        path: '',
        name: 'layout',
        component: () => import('@/views/layout/index.vue'),
        redirect: '/dashboard',
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                component: () => import('@/views/dashboard/index.vue'),
                meta: {title: '首页'}
            },
            {
                path: '/personal/profile',
                component: () => import('@/views/personal/profile/index.vue'),
                meta: {title: '个人信息'}
            },
            {
                path: '/personal/changePassword',
                component: () => import('@/views/personal/change_password/index.vue'),
                meta: {title: '修改密码'}
            },
            {
                path: '/crm/customer',
                component: () => import('@/views/crm/customer/index.vue'),
                meta: {title: '客户管理'}
            },
            {
                path: '/crm/customer/add',
                component: () => import('@/views/crm/customer/add.vue'),
                meta: {
                    title: '新增客户',
                    parent: {title: '客户管理', path: '/crm/customer'}
                }
            },
            {
                path: '/crm/customer/edit',
                component: () => import('@/views/crm/customer/edit.vue'),
                meta: {
                    title: '修改客户',
                    parent: {title: '客户管理', path: '/crm/customer'}
                }
            },
            {
                path: '/crm/customer/info',
                component: () => import('@/views/crm/customer/info.vue'),
                meta: {
                    title: '客户详情',
                    parent: {title: '客户管理', path: '/crm/customer'}
                }
            },
            {
                path: '/crm/customer/follow',
                component: () => import('@/views/crm/customer/follow.vue'),
                meta: {
                    title: '客情跟踪',
                    parent: {title: '客户管理', path: '/crm/customer'}
                }
            },
            {
                path: '/crm/order',
                component: () => import('@/views/crm/order/index.vue'),
                meta: {title: '订单管理'}
            },
            {
                path: '/crm/project_order',
                component: () => import('@/views/crm/project_order/engineering_anagement.vue'),
                meta: {
                    title: '工程订单管理',
                    parent: {title: '财务管理'}
                }
            },
            {
                path: '/crm/project_order/add_engineering_anagement',
                component: () => import('@/views/crm/project_order/add_engineering_anagement.vue'),
                meta: {
                    title: '录入工程订单',
                    parent: {title: '财务管理'}
                }
            },
            {
                path: '/crm/project_order/select_engineering_anagement',
                component: () => import('@/views/crm/project_order/select_engineering_anagement.vue'),
                meta: {
                    title: '查看工程订单',
                    parent: {title: '财务管理'}
                }
            },
            {
                path: '/crm/detail',
                component: () => import('@/views/crm/order/order.vue'),
                meta: {
                    title: '订单修改',
                    parent: {title: '订单管理', path: '/crm/order'}
                }
            },
            {
                path: '/crm/orderDetail',
                component: () => import('@/views/crm/order/details.vue'),
                meta: {
                    title: '订单详情',
                    parent: {title: '订单管理', path: '/crm/order'}
                }
            },
            {
                path: '/crm/project',
                component: () => import('@/views/crm/project/project.vue'),
                meta: {title: '工程管理'}
            },
            {
                path: '/crm/drawing_audit',
                component: () => import('@/views/crm/drawing_audit/drawing_audit.vue'),
                meta: {
                    title: '图纸审核',
                    parent: {title: '工程管理', path: '/crm/project'}
                }
            },
            {
                path: '/crm/staff',
                component: () => import('@/views/crm/staff/staff.vue'),
                meta: {title: '员工管理'}
            },
            {
                path: '/crm/staff/staff_add',
                component: () => import('@/views/crm/staff/staff_add.vue'),
                meta: {
                    title: '新增员工',
                    parent: {title: '员工管理', path: '/crm/staff'}
                }
            },
            {
                path: '/crm/staff/staff_update',
                component: () => import('@/views/crm/staff/staff_update.vue'),
                meta: {
                    title: '编辑员工',
                    parent: {title: '员工管理', path: '/crm/staff'}
                }
            },
            {
                path: '/crm/dept',
                component: () => import('@/views/crm/dept/dept.vue'),
                meta: {title: '部门管理'}
            },
            {
                path: '/crm/dept/dept_add',
                component: () => import('@/views/crm/dept/dept_add.vue'),
                meta: {
                    title: '新增部门',
                    parent: {title: '部门管理', path: '/crm/dept'}
                }
            },
            {
                path: '/crm/dept/dept_update',
                component: () => import('@/views/crm/dept/dept_update.vue'),
                meta: {
                    title: '编辑部门',
                    parent: {title: '部门管理', path: '/crm/dept'}
                }
            },
            {
                path: '/crm/data_report',
                component: () => import('@/views/crm/data_report/index.vue'),
                meta: {title: '数据报表'}
            },
            {
                path: '/crm/business_indicators',
                component: () => import('@/views/crm/business_indicators/index.vue'),
                meta: {title: '业务指标'}
            },
            {
                path: '/crm/earnest_money',
                component: () => import('@/views/crm/earnest_money/license_plate.vue'),
                meta: {title: '定金管理'}
            },
            {
                path: '/crm/earnest_money/add_money',
                component: () => import('@/views/crm/earnest_money/add_money.vue'),
                meta: {
                    title: '录入定金',
                    parent: {title: '部门管理', path: '/crm/add_money'}
                }
            },
            {
                path: '/crm/earnest_money/select_money',
                component: () => import('@/views/crm/earnest_money/select_money.vue'),
                meta: {
                    title: '查询定金',
                    parent: {title: '部门管理', path: '/crm/add_money'}
                }
            },
            {
                path: '/crm/chargeback',
                component: () => import('@/views/crm/chargeback/RefundTableView.vue'),
                meta: {title: '退单管理'}
            },
            {
                path: '/handleRefund',
                component: () => import('@/views/crm/chargeback/HandleRefundView.vue')
            },
            {
                path: '/checkRefund',
                component: () => import('@/views/crm/chargeback/CheckRefundView.vue')
            },
            {
                path: '/refundAudit1',
                component: () => import('@/views/crm/chargeback/RefundAuditView1.vue')
            },
            {
                path: '/refundAudit2',
                component: () => import('@/views/crm/chargeback/RefundAuditView2.vue')
            },
            {
                path: '/system/user',
                component: () => import('@/views/system/user/index.vue'),
                meta: {title: '用户管理'}
            },
            {
                path: '/system/role',
                component: () => import('@/views/system/role/index.vue'),
                meta: {title: '角色管理'}
            },
            {
                path: '/system/menu',
                component: () => import('@/views/system/menu/index.vue'),
                meta: {title: '菜单管理'}
            },
            {
                path: '/notice',
                component: () => import('@/views/notice/index.vue'),
                meta: {
                    title: '系统公告',
                }
            },
            {
                path: '/system/notice',
                component: () => import('@/views/system/notice/index.vue'),
                meta: {
                    title: '系统公告',
                    parent: {title: '系统管理'}
                }
            },
            {
                path: '/system/notice/add',
                component: () => import('@/views/system/notice/add.vue'),
                meta: {
                    title: '发布公告',
                    parent: {title: '系统公告', path: '/system/notice'}
                }
            },
            {
                path: '/system/notice/detail',
                component: () => import('@/views/system/notice/detail.vue'),
                meta: {
                    title: '公告详情',
                    parent: {title: '系统公告', path: '/system/notice'}
                }
            },
            {
                path: '/system/dict',
                component: () => import('@/views/system/dict/index.vue'),
                meta: {title: '字典管理'}
            },
            {
                path: '/system/dict/option',
                component: () => import('@/views/system/dict/option/index.vue'),
                meta: {title: '字典项列表'}
            },
            {
                path: '/system/log',
                component: () => import('@/components/BlankRoute/index.vue'),
                meta: {title: '日志管理'},
                redirect: 'noRedirect',
                children: [
                    {
                        path: '/system/log/operate',
                        component: () => import('@/views/system/log/OperateLog.vue'),
                        meta: {title: '操作日志'}
                    }
                ]
            },
        ]
    },
    {
        path: '/login',
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/403',
        component: () => import('@/views/_error/403.vue')
    },
    {
        path: '/404',
        component: () => import('@/views/_error/404.vue')
    },
    {
        path: '/500',
        component: () => import('@/views/_error/500.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior: () => {
        return {top: 0}
    }
})

// 免登录白名单
const whiteList = ['/login', '/403', '/404', '/500']

// 路由导航前置守卫
router.beforeEach(async (to, from, next) => {
    // 设置页面标题
    document.title = to.meta.title || '装企CRM管理系统'
    // 判断是否登录
    if (!localStorage.getItem('token')) {
        // 在免登录白名单中的直接放行，其它的重定向到登录页面
        whiteList.includes(to.path) ? next() : next(`/login?redirect=${to.path}`)
        return
    }
    // 如果没有找到匹配的路由，转到404页面，否则直接进入
    to.matched.length === 0 ? next('/404') : next()
})

export default router
